<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        let ws
        function connect(){
            if ('WebSocket' in window) {
                // 创建websocket连接
                ws = new WebSocket( "ws://" + location.host + "/u/websocket/message?token=" +document.getElementById("token").value);
                // 成功连接的时候推送一条消息，此时服务端就可以开始推送数据了
                ws.onopen = () => {
                    console.log('websocket success---');
                    //下面是单聊,type=2
                    ws.send("{\"room_id\":\"5180f89db02046eb887b9dd9e2297941\",\"content\":\"gongcaihua\",\"type\":2,\"media\":1}");
                    //下面是群聊,type=1
                    //     ws.send("{\"to\":\"3c50e612ed1145a99e3e7166d8adcef0\",\"content\":\"gongcaihua\",\"type\":1,\"media\":1}");

                }
                ws.onmessage = (message) => {
                    let data = message.data;
                    console.log('get websocket message---', data);
                    //  socket.send("{\"room_id\":\"58dc0b6ec3474835a947e12f89ac6fb2\",\"content\":\"gongcaihua\",\"type\":1}");
                }
                ws.onclose = function(e){
                    console.log("close,"+e.code);
                }



                ws.onerror = () => {
                    console.error('websocket fail');
                }
            } else {
                console.error('dont support websocket');
            };
        };
        function sendDemo(){
            console.log("dddddddddd")
            ws.send(document.getElementById("messageInput").value);

        }

        // 定义发送消息的函数
        function sendMessage() {
            // 这里可以是任何你想发送的数据
            console.log("dddddddddd")
            // 发送消息
            if (ws!=null) {
                ws.send(document.getElementById("messageInput").value);
            }
        }

        // 设置定时器，例如每5秒发送一次消息
        setInterval(sendMessage, 10000);
    </script>

</head>
<body>

Token:   {{.Token}}
<br>
UserId: {{.UserId}}
<br>
<input type="text" size="100" id="token"  value="{{.Token}}"> <button id="sendButton"  onclick="connect()"> 连接</button>

<input type="text" size="100" id="messageInput"  value='{"room_id":"888888888888888888","content":"gongcaihua","type":1,"media":1}'>
<button id="sendButton"  onclick="sendDemo()">发送</button>
<br>
<a href="./u/chat/list?page_size=10&page_index=1&room_id=888888888888888888&token={{.Token}}">获得房间聊天内容</a>
<br>
<a href="./u/user/query?account=gongcaihua22&token={{.Token}}">用户信息</a>
<br>
<a href="./u/user/findFriendsList?account=gongcaihua22&token={{.Token}}">1对1聊天列表</a>
<br>
<a href="./client">登录用户数</a><br>
<a href="./insertManyRegister?size=100000">插入用户数据</a><br>
<a href="./getReport?reportType=login:">登录数/秒</a><br>
<a href="./getReport?reportType=send:">消息/秒</a><br>
</body>
</html>